package com.zjmzxfzhl.modules.flowable.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.flowable.entity.FlowableForm;
import com.zjmzxfzhl.modules.flowable.service.FlowableFormService;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.dshubs.dto.SysDictDataDTO;
import io.dshubs.dto.SysDictTypeDTO;
import io.dshubs.feign.DictFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import static com.alibaba.fastjson.JSON.parseArray;
import static com.alibaba.fastjson.JSON.parseObject;

/**
 * 流程Controller
 *
 * @author 庄金明
 */
@Slf4j
@Api(tags = "流程表单")
@RestController
@RequestMapping("/flowable/form")
public class FlowableFormController extends BaseController {
    @Autowired
    private FlowableFormService flowableFormService;

    @Autowired
    private DictFeignClient dictFeignClient;
    @ApiOperation("查询分页")
    @GetMapping(value = "/list")
    public Result list(FlowableForm flowableForm, @RequestParam Integer current, @RequestParam Integer size) {
        IPage<FlowableForm> pageList = flowableFormService.list(new Page<>(current, size), flowableForm);
        return Result.ok(pageList);
    }

    @ApiOperation("查询详情")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        FlowableForm flowableForm = flowableFormService.getById(id);
        return Result.ok(flowableForm);
    }

    @ApiOperation("新增流程表单")
    @PostMapping(value = "/save")
    public Result save(@Validated(AddGroup.class) @RequestBody FlowableForm flowableForm) {
        String formJson = flowableForm.getFormJson();
        Map formMap = parseObject(formJson,Map.class);
        SysDictTypeDTO sysDictTypeDTO = new SysDictTypeDTO();
        sysDictTypeDTO.setRemark("全宗基础字段字典同步");
        sysDictTypeDTO.setDictName("字段类型");
        sysDictTypeDTO.setDictType("BASE_ZDLX");
        Long tenantCode = flowableForm.getTenantCode();
        if(tenantCode == null) {
            tenantCode = SecurityUser.getTenantCode();
        }
        sysDictTypeDTO.setDictName(tenantCode.toString());
        sysDictTypeDTO.setId(IdWorker.getId());
        dictFeignClient.save(sysDictTypeDTO);
        Object[] fields = parseArray(formMap.get("fields").toString()).toArray();
        for (Object fieldObj : fields) {
            String fieldStrJson = String.valueOf(fieldObj);
            Map map = parseObject(fieldStrJson, Map.class);
            SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
            Map config__ = parseObject(String.valueOf(map.get("__config__")), Map.class);
            sysDictDataDTO.setDictLabel("FIELD");
            sysDictDataDTO.setDictValue(String.valueOf(map.get("__vModel__")));
            sysDictDataDTO.setDictTypeId(sysDictTypeDTO.getId());
            sysDictDataDTO.setDictType(sysDictTypeDTO.getDictType());
            sysDictDataDTO.setRemark(String.valueOf(config__.get("label")));
            dictFeignClient.saveData(sysDictDataDTO);
        }

        flowableFormService.save(flowableForm);
        return Result.ok();
    }

    @ApiOperation("修改流程表单")
    @PutMapping(value = "/update")
    public Result update(@Validated(UpdateGroup.class) @RequestBody FlowableForm flowableForm) {
        flowableFormService.removeById(flowableForm.getFormKey());
        save(flowableForm);
        return Result.ok();
    }

    @ApiOperation("批量删除")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam @NotNull(message = "id不能为空！") String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids 不能为空!");
        }
        String[] idsArr = ids.split(",");
        if (idsArr.length > 1) {
            flowableFormService.removeByIds(Arrays.asList(idsArr));
        } else {
            flowableFormService.removeById(idsArr[0]);
        }
        return Result.ok();
    }
}
